package com.base.essearch.page;

/**
 * @author lr
 */
public class SimplePage implements Paginable {
    public static final int DEF_COUNT = 20;

    public SimplePage() {
    }

    public SimplePage(int pageNo, int pageSize, int totalCount) {
        if (totalCount <= 0) {
            this.total = 0;
        } else {
            this.total = totalCount;
        }
        if (pageSize <= 0) {
            this.pageSize = DEF_COUNT;
        } else {
            this.pageSize = pageSize;
        }
        if (pageNo <= 0) {
            this.pageNum = 1;
        } else {
            this.pageNum = pageNo;
        }
        if ((this.pageNum - 1) * this.pageSize >= totalCount) {
            this.pageNum = totalCount / pageSize;
            if (this.pageNum == 0) {
                this.pageNum = 1;
            }
        }
    }

    /**
     * 调整分页参数，使合理化
     */
    public void adjustPage() {
        if (total <= 0) {
            total = 0;
        }
        if (pageSize <= 0) {
            pageSize = DEF_COUNT;
        }
        if (pageNum <= 0) {
            pageNum = 1;
        }
        if ((pageNum - 1) * pageSize >= total) {
            pageNum = total / pageSize;
        }
    }

    @Override
    public int getPageNum() {
        return pageNum;
    }

    @Override
    public int getPageSize() {
        return pageSize;
    }

    @Override
    public int getTotal() {
        return total;
    }

    @Override
    public int getTotalPage() {
        int totalPage = total / pageSize;
        if (total % pageSize != 0 || totalPage == 0) {
            totalPage++;
        }
        return totalPage;
    }

    @Override
    public boolean isFirstPage() {
        return pageNum <= 1;
    }

    @Override
    public boolean isLastPage() {
        return pageNum >= getTotalPage();
    }

    @Override
    public int getNextPage() {
        if (isLastPage()) {
            return pageNum;
        } else {
            return pageNum + 1;
        }
    }

    @Override
    public int getPrePage() {
        if (isFirstPage()) {
            return pageNum;
        } else {
            return pageNum - 1;
        }
    }

    protected int total = 0;
    protected int pageSize = 20;
    protected int pageNum = 1;

    public void setTotal(int total) {
        this.total = total;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }


    protected int filterNo;

    public int getFilterNo() {
        return filterNo;
    }

    public void setFilterNo(int filterNo) {
        this.filterNo = filterNo;
    }


}
